package LeetCode;

/**
 * @Author: Dijah
 * @Date: 2023/4/7 11:01
 * 两个字符串相乘
 * @Desc: https://leetcode.cn/submissions/detail/350327610/
 */
public class code43_multiply {

    public static String multiply(String num1, String num2) {

        if("0".equals(num1) || "0".equals(num2)){
            return "0";
        }
        int num1Length = num1.length();
        int num2Length = num2.length();
        int[] ansArr = new int[num1Length+num2Length];
        for(int i = num1Length - 1;i>=0; i--){
            int n1 = num1.charAt(i) - '0';
            for (int j = num2Length - 1; j >= 0;j--){
                int n2 = num2.charAt(j) - '0';
                int sum = (ansArr[i + j +1] + n1 * n2);
                ansArr[i + j + 1] = sum % 10;
                ansArr[i + j] += sum / 10;
            }
        }
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i<ansArr.length; i++){
            //处理等于0 的
            if(i == 0 && ansArr[i] == 0){
                continue;
            }
            sb.append(ansArr[i]);
        }
        return  sb.toString();
    }

    public static void main(String[] args) {
        String num1 = "1234";
        String num2 = "5678";
        String multiply = multiply(num1, num2);
        System.out.println(multiply);
    }
}
